import { Injectable } from '@angular/core';
import { UnitsProvider } from '../../providers/units/units';
/*
  Generated class for the PayProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/

declare let Wechat;  // 此处声明plugin.xml中clobbers对应的值
declare let Alipay;  // 此处声明plugin.xml中clobbers对应的值
@Injectable()
export class PayProvider {

  constructor(
    public units: UnitsProvider) {
    console.log('Hello PayProvider Provider');
  }

  public dopay(payid: any, fields: any) {
    let promise = new Promise((success, fail) => {
      //微信支付
      if (payid == 1) {
        //微信app支付
        if (this.units.isApp()) {
          return this.doWechatApp(fields).then(res1 => {
            //支付成功
            if (success)
              success(res1);
          }, res2 => {
            //支付失败
            if (fail)
              fail(res2);
          });
        }
        //微信公众号支付
        else if (this.units.isWechat()) {
          return this.doWechatWap(fields).then(res1 => {
            //支付成功
            if (success)
              success(res1);
          }, res2 => {
            //支付失败
            if (fail)
              fail(res2);
          });
        }
        //微信wap支付
        else {
          return this.doWechatWap(fields).then(res1 => {
            //支付成功
            if (success)
              success(res1);
          }, res2 => {
            //支付失败
            if (fail)
              fail(res2);
          });
        }
      }
      //支付宝支付
      if (payid == 2) {
        //支付宝app支付
        if (this.units.isApp()) {
          return this.doAlipayApp(fields).then(res1 => {
            //支付成功
            if (success)
              success(res1);
          }, res2 => {
            //支付失败
            if (fail)
              fail(res2);
          });
        }
        //支付宝wap支付 
        else {
          return this.doAlipayWap(fields).then(res1 => {
            //支付成功
            if (success)
              success(res1);
          }, res2 => {
            //支付失败
            if (fail)
              fail(res2);
          });
        }
      }
    });
    //异步回调
    return promise;
  }

  //参数组装（微信）
  private unescaoeWxObject(fields: any) {
    //参数
    let params = {
      partnerid: fields.partnerid, //微信支付分配的商户号
      prepayid: fields.prepayid, //预支付交易会话ID
      noncestr: fields.noncestr, //随机字符串
      timestamp: fields.timestamp, //时间戳
      sign: fields.sign //签名
    };
    return params;
  }

  //支付宝支付(wap)
  private doAlipayWap(apyObj: any) {
    let promise = new Promise((success, fail) => {  });
    //异步回调
    return promise;
  }

  //支付宝支付(app)
  private doAlipayApp(apyObj: any) {
    let promise = new Promise((success, fail) => {
      Alipay.Base.pay(apyObj, function (result) {
        //支付成功
        if (success)
          success(result);
      }, function (error) {
        //支付失败
        if (fail)
          fail(error);
      });
    });
    //异步回调
    return promise;
  }
  //微信支付(wap)
  private doWechatWap(apyObj: any) {
    let promise = new Promise((success, fail) => { 
      
     });
    //异步回调
    return promise;
  }
  //微信支付(app)
  private doWechatApp(apyObj: any) {
    let that = this;
    let promise = new Promise((success, fail) => {
      console.log("pay:doWechatApp"+apyObj);
      let payParams = that.unescaoeWxObject(apyObj);
      Wechat.sendPaymentRequest(payParams, (result) => {
        //支付成功
        console.log("pay:success");
        if (success)
          success(result);
      }, (error) => {
        //支付失败
        console.log("pay:fail");
        if (fail)
          fail(error);
      });
    });
    //异步回调
    return promise;
  }

}
